package cn.zy.company.servlet.admin;



import cn.zy.company.entity.News;
import cn.zy.company.entity.PageObj;
import cn.zy.company.entity.User;
import cn.zy.company.service.NewsService;
import cn.zy.company.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


@WebServlet("/admin/showNews")
public class AdminShowNewsServlet extends HttpServlet {
    PageObj<News> pageObj = new PageObj<News>();
    UserService userService=new UserService();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String page = req.getParameter("page");
            String size = req.getParameter("size");
            if (page != null && page != "") {
                pageObj.setPage(Integer.parseInt(page));
            }
            if (size != null && size != "") {
                pageObj.setSize(Integer.valueOf(size));
            }
            String id = req.getParameter("id");
            NewsService newsService=new NewsService();
            if (id != null && id != "") {
                PageObj<News> obj = newsService.findById(Integer.valueOf(id));
                req.setAttribute("dateList", obj);
                List<User> users = userService.findAll();
                req.setAttribute("users",users);
                req.setAttribute("id",id);
                req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
                return;
            }
            String name = req.getParameter("name");
            if (name != null && name != "") {
                PageObj<News> obj = newsService.find(name, pageObj);
                req.setAttribute("dateList", obj);
                List<User> users = userService.findAll();
                req.setAttribute("users",users);
                req.setAttribute("name",name);
                req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
                return;
            }
            String userId = req.getParameter("userId");
            if(userId!=null&&userId!=""){
                PageObj<News> obj = newsService.findByUserId(userId, pageObj);
                req.setAttribute("dateList", obj);
                List<User> users = userService.findAll();
                req.setAttribute("users",users);
                req.setAttribute("userId",userId);
                req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
                return;
            }
            String date = req.getParameter("date");
            if(date!=null&&date!=""){
                PageObj<News> obj = newsService.findByDate(date, pageObj);
                req.setAttribute("dateList", obj);
                List<User> users = userService.findAll();
                req.setAttribute("users",users);
                req.setAttribute("date",date);
                req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
                return;

            }
            PageObj<News> all = newsService.findAll(pageObj);
            req.setAttribute("dateList", all);
            List<User> users = userService.findAll();
            req.setAttribute("users",users);
            req.getRequestDispatcher("/WEB-INF/jsp/admin/showNews.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
